import Vue from 'vue'
import Router from 'vue-router'
/* Layout 布局文件 */
import Layout from '@/layout'

Vue.use(Router)

/*
  定义组件名称和组件对象的map对象
 */
export const componentMap = {
  'layout': require('@/layout').default,
  '/system/user': resolve => require(['@/views/system/user/index.vue'], resolve).default,
  '/system/dept': resolve => require(['@/views/system/dept/index.vue'], resolve).default,
  '/system/menu': resolve => require(['@/views/system/menu/index.vue'], resolve).default,
  '/system/role': resolve => require(['@/views/system/role/index.vue'], resolve).default,
  '/monitor/job': resolve => require(['@/views/monitor/job/index.vue'], resolve).default,
  '/monitor/operLog': resolve => require(['@/views/monitor/operLog/index.vue'], resolve).default,
  '/monitor/cache': resolve => require(['@/views/monitor/cache/index.vue'], resolve).default,
  '/weixin/wxUser': resolve => require(['@/views/weixin/wxUser/index.vue'], resolve).default,
  '/mall/goodsCategory': resolve => require(['@/views/mall/goodsCategory/index.vue'], resolve).default,
  '/mall/goodsSpu': resolve => require(['@/views/mall/goodsSpu/index.vue'], resolve).default,
  // '/mall/orderInfo': resolve => require(['@/views/mall/orderInfo/index.vue'], resolve).default,
}

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   设置菜单是否隐藏（不显示在左边菜单，缺省值false）
 * alwaysShow: true               设置显示一级菜单上，是否总是显示，true表示总是显示
 *                                如果为false，一级菜单会在有多个子菜单的情况下才显示
 * redirect: noRedirect           如果设置为noRedirect，面包屑不会重定向
 * name:'router-name'             路由名称，必须设置，且唯一
 * meta : {
    roles: ['admin','editor']    允许路由的角色列表
    title: 'title'               显示在左边导航和面包屑上的名称
    icon: 'svg-name'/'el-icon-x' 图标名称
    noCache: true                页面是否不要缓存，缺省值为false
    affix: true                  标签页面是否总是显示（不能关闭tags-vies）
    breadcrumb: false            缺省值为true，是否以面包屑显示页面
    activeMenu: '/example/list'  在左边菜单行高亮显示
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */

// 无需权限路由
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: (resolve) => require(['@/views/redirect'], resolve)
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/index',
    hidden: true,
    children: [{
      path: '/index',
      name: 'index',
      component: () => import('@/views/index/index'),
      meta: {
        title: '系统首页',
        affix: true,
      }
    }]
  },
  {
    path: '/user',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'profile',
        component: (resolve) => require(['@/views/system/user/profile/index'], resolve),
        name: 'profile',
        meta: {title: '个人中心', icon: 'user'}
      }
    ]
  },
  {
    path: '/job',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'log',
        component: (resolve) => require(['@/views/monitor/job/log'], resolve),
        name: 'jobLog',
        meta: {title: '调度日志'}
      }
    ]
  },
  {
    path: '/success',
    component: () => import('@/views/public/success.vue'),
    hidden: true
  },
  {
    path: '/error',
    component: () => import('@/views/public/error.vue'),
    hidden: true
  },
]

const createRouter = () => new Router({
  mode: 'history', // 路由模式
  base: 'mall-ui', // 项目根路径
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRoutes,
})

const router = createRouter()


export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
